// 小仓库:layout组件相关配置仓库
import {defineStore} from 'pinia'
import router from "@/router";
import {getMenuList} from "@/utils/token.ts";

const useMenuRouter = defineStore('menuRouter', {
    state: () => {
        return {
            menuList: getMenuList(),
            tagViewList: []
        }
    },
    actions: {
        // 用于添加tagView
        addTagView(tagView: any) {
            // 判断当前tagView是否已经存在
            const result = this.tagViewList.find((item: any) => item.path == tagView.path)
            if (!result && tagView.path != '/login') {
                this.tagViewList.push(tagView)
            }
        },
        removeTagView(tagView: string) {
            // 判断当前tagView是否已经存在
            const index = this.tagViewList.findIndex((item: any) => item.path == tagView)
            if (index != -1) {
                // 如果删除的是当前路由，需要跳转到上一个路由
                const prevPath: string = router.currentRoute.value.path
                if (this.tagViewList.length > 1 && this.tagViewList[index].path === prevPath) {
                    router.push(this.tagViewList[index + (!index ? 1 : -1)].path)
                }
                this.tagViewList.splice(index, 1);
            }
        },
        clearTagView() {
            this.tagViewList = []
        }
    }

})

export default useMenuRouter
